package servlet.Article;

import DAO.ArticleDAO;
import Model.Article;
import Model.User;
import appexception.AppException;
import service.BaseService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: wsn_blog
 * @description
 * @author:王松年
 * @creat: 2021-07-12 15:28:23
 **/

@WebServlet("/article")
public class ArticleListServlet extends BaseService {

    @Override
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        //从session获取用户
        HttpSession session = req.getSession(false);
        if (session == null) {
            throw new AppException("ART001", "未登录");
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            throw new AppException("ART001", "未登录");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user", user);
        //用户登录，查询文章
        List<Article> articleList = ArticleDAO.selectByUserId(user.getId());
        map.put("articleList", articleList);
        json.setData(map);
    }
}
